package csc.daonjpa.java.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.domain.Customer;


@Controller
public class CustomerController {

	@RequestMapping(value="/loginPage", method = RequestMethod.GET)
	public String viewLogin(HttpServletRequest request, Model model){
		return "login";
	}
//	@RequestMapping(value="/login")
//	public String viewLogin1(HttpServletRequest request, Model model){
//		return "login.html";
//	}
	@RequestMapping(value="/checkLogin")
	public String loginCustomer(HttpServletRequest request, Model model){
		String loginId = request.getParameter("loginId");
		String password = request.getParameter("password");
		CustomerDAO customerDao = new CustomerDAO();
		Customer customer = customerDao.checkLogin(loginId, password);
		
		if(customer !=null){
			HttpSession session = request.getSession();
			session.setAttribute("username", loginId);
			return "forward:/home.html";
		}else{
			System.out.println("Please check your username or password again!");
			model.addAttribute("loginMessage", "Please check your username or password again!");
			return "login";
		}
	}
	
	
	@RequestMapping(value="/modifyCustomer")
	public String modifyCustomer(HttpServletRequest request, Model model){
		Customer customer = new Customer();
		customer.setPassword(request.getParameter("password"));
		customer.setFirstname(request.getParameter("firstname"));
		customer.setLastname(request.getParameter("lastnane"));
		customer.setAddress(request.getParameter("address"));
		customer.setPhone(request.getParameter("phone"));
		
		CustomerDAO customerDao = new CustomerDAO();
		
		if(customerDao.modifyCustomer(customer)){
			System.out.println("Modify successed!");
			return "forward:/home.html";
		}else{
			System.out.println("Modify failed!");
			return "modifyCustomer";
		}
		
	}
}
